package com.qf.index;

import java.sql.SQLException;


public class WosToPmSearch {

    public static void main(String[] args) throws SQLException {

        String kw = "(AD=((Shandong First Medical University OR Shandong First Med Univ) SAME (Qianfoshan Hosp OR Qian Foshan Hosp OR Qian Fe Shan Hosp OR Qian Fo Shan Hosp OR Qianfo Mt Hosp OR Qianfo Shan Hosp OR Qianfeshan Hosp OR Affiliated Hosp 1, OR Hosp 1, OR Hosp Affiliated 1,) SAME (jinan OR ji'nan OR 16766 Jingshi Rd)) OR AD=((Qianfoshan Hosp OR Qian Foshan Hosp OR Qian Fe Shan Hosp OR Qian Fo Shan Hosp OR Qianfo Mt Hosp OR Qianfo Shan Hosp OR Qianfeshan Hosp) SAME (jinan OR ji'nan OR 16766 Jingshi Rd))) NOT (AD=((Shandong University) SAME (Qianfoshan Hosp))) NOT AD=Shandong NOT AD=(Qianfoshan Hosp)";
//        String kw = "AD=((Chinese Acad Med Sci) SAME (Skin Dis Hosp OR Skin Dis & STD Hosp OR Dermatol Hosp))";
        kw ="AD=Shanghai Jiao Tong University  SAME Renji Hospital OR Ren Ji Hospital OR RENJI HOSP";
        String s = wosToPmSearch(kw);
        System.out.println(s);
    }

    public static String changeAd(String ad) {
        // AD检索只有三种形式，一种是含SAME，此种需要前后加括号，并把SAME变为AND，第二种是含括号，且可能是有重复的括号（比如，两个左括号），而理论上左括号和右括号应该都只有一个。第三种是没有括号，此种需要前后加上括号。这三种均需要删除"AD="即"Đ"。
        if (ad.contains("Á SAME À")) {
            ad = "À" + ad.replace("Á SAME À", "ÁÁǍĐÀÀ").replace("Ŏ", "[AD]Ŏ").replace("ÁÁ", "[AD]ÁÁ").replace("Đ", "") + "Á";
        } else if (ad.startsWith("ĐÀ") && ad.endsWith("Á")) {
            ad = ad.replaceAll("À+", "À").replaceAll("Á+", "Á").replace("Á", "[AD]Á").replace("Đ", "");
        } else if (ad.startsWith("Đ") && !ad.contains("À") && !ad.contains("Á")) {
            ad = "À" + ad.substring(1) + "[AD]Á";
        }
        return ad;
    }

    public static String wosToPmSearch(String search) {
        // wos的检索式转为pm的检索式，仅AD字段，其它字段不支持。除了逻辑检索词全部是大写的外，其它检索词保留原来的大小写状态。
        if (null == search || !search.toUpperCase().contains("AD")) {
            return "";
        }
        String s = search.replace("﹚", ") ").replace("﹙", " (").replace("）", ") ").replace("（", " (").replace("〓", "=").replace("═", "=").replace("﹦", "=")
                .replace("＝", "=").replaceAll(" +", " ").replace(" )", ")").replace("( ", "(").replace(" =", "=").replace("= ", "=").replace("(", "À")
                .replace(")", "Á").replaceAll("(?i)AD=", "Đ").replaceAll("(?i) OR ", "Ŏ").replaceAll("(?i) AND ", "Ǎ").replaceAll("(?i) NOT ", "Ň").trim();
        for (int i = 0; i < s.length(); i++) {
            String ad = getAd(s);
            if (!ad.isEmpty()) {
                int l = s.indexOf(ad);
                String f = s.substring(0, l);
                String e = s.substring(l + ad.length());
                s = f + changeAd(ad) + e;
            } else {
                break;
            }
        }
        return s.replace("Đ", "AD=").replace("Ŏ", " OR ").replace("Ǎ", " AND ").replace("Ň", " NOT ").replace("À", "(").replace("Á", ")");
    }

    public static String getAd(String ad) {
        // 获取含有AD检索式的语句，比如：AD=(Chongqing Canc Hosp) OR AD=(Chongqing Tumor Hosp)，如果左括号和右括号的数量相等且大于0个即认为结束，或者不含有括号而直接遇到了逻辑检索词（AND、OR、NOT）也认为是结束，逻辑词结束时不能含有逻辑词本身。
        if (ad.contains("Đ")) {
            String[] split = ad.substring(ad.indexOf("Đ")).split("");
            int l = 0;
            int r = 0;
            StringBuilder result = new StringBuilder();
            for (String s : split) {
                if ("À".equals(s)) {
                    l++;
                    result.append(s);
                } else if ("Á".equals(s)) {
                    r++;
                    result.append(s);
                    if (l > 0 && l == r) {
                        return result.toString();
                    }
                } else if (l == 0 && r == 0 && ("Ǎ".equals(s) || "Ň".equals(s) || "Ŏ".equals(s))) {
                    return result.toString();
                } else {
                    result.append(s);
                }
            }
        }
        return "";
    }


}
